﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileMonitor.Service.Prototype;
using System.IO;

namespace FileMonitor.Service.Tracking
{
    public class MoveFileTracking:ITracking
    {
        public void StartProcess(FileMonitorBase process)
        {
 
        }

        public void StopProcess(FileMonitorBase process)
        { }

        public void InsertNewFile(FileMonitorBase process, string filePath)
        {
        }

        public void CompleteFile(FileMonitorBase process, string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            string backupPath = Path.Combine(process.BaseConfiguration.BackUpPath, fileName); 
            if (System.IO.File.Exists(backupPath))
            {
                System.IO.File.Delete(backupPath);
            }

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Move(filePath, backupPath);
            }
        }

        public void PreocessErrorFile(FileMonitorBase process, string filePath, Exception ex)
        {
            string fileName = Path.GetFileName(filePath);
            string backupPath = process.BaseConfiguration.BackUpPath;
            string erroFileName = fileName + ".Error";
            string errorBackupPath = Path.Combine(backupPath, erroFileName);
            if (System.IO.File.Exists(errorBackupPath))
            {
                System.IO.File.Delete(errorBackupPath);
            }
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Move(filePath, errorBackupPath);
            }
        }

        public string TrackingName
        {
            get
            {
                return "MoveFileTracking";
            }
        }

        public int OrderNo
        {
            get
            {
                return 1;
            }
        }
    }
}
